import Axios from "axios";

const axios = Axios.create({
  baseURL: "https://apis.map.qq.com",
});

const { QQ_MAP_KEY } = process.env;

export async function searchPlace(body) {
  const { city, keyword, lat, lng, distance = 3000 } = body;
  let boundary;
  if (city) {
    boundary = `region(${city},0)`;
  } else if (lat && lng) {
    boundary = `nearby(${lat},${lng},${distance})`;
  } else {
    throw "缺少ciy 或 lat&lng";
  }
  if (!keyword) {
    throw "缺少keyword";
  }
  const { data } = await axios.get("/ws/place/v1/search", {
    params: {
      key: QQ_MAP_KEY,
      boundary,
      keyword,
    },
  });
  if (data.status != 0) {
    throw data.message;
  }
  return {
    count: data.count,
    rows: data.data,
  };
}
